package LeetCode.month02.day57;

public class No130 {
    boolean[][] res;
    public void solve(char[][] board) {
        res = new boolean[board.length][board[0].length];
        int m = board.length;
        int n = board[0].length;
        for (int i = 0; i < m; i++) {
            if(board[i][0]=='O'&&!res[i][0]) dfs(board,i,0);
            if(board[i][n-1]=='O'&&!res[i][n-1]) dfs(board,i,n-1);
        }
        for (int i = 0; i < n; i++) {
            if(board[0][i]=='O'&&!res[0][i]) dfs(board,0,i);
            if(board[m-1][i]=='O'&&!res[m-1][i]) dfs(board,m-1,i);
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(res[i][j]) board[i][j]='O';
                else board[i][j]='X';
            }
        }
    }

    public void dfs(char[][]board,int m , int n){
        if(m<0||m>=board.length||n<0||n>=board[0].length||board[m][n]!='O'||res[m][n]){
            return;
        }
        res[m][n] = true;
        dfs(board,m+1,n);
        dfs(board,m-1,n);
        dfs(board,m,n+1);
        dfs(board,m,n-1);
    }
}
